package cn.iocoder.yudao.module.happycore.service.oxdirectory;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.happycore.constants.DirectoryType;
import cn.iocoder.yudao.module.happycore.controller.admin.oxdirectory.vo.OxDirectoryPageReqVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxdirectory.vo.OxDirectoryRespVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxdirectory.vo.OxDirectorySaveReqVO;
import cn.iocoder.yudao.module.happycore.dal.dataobject.oxdirectory.OxDirectoryDO;
import cn.iocoder.yudao.module.happycore.dal.dataobject.oxproject.OxProjectDO;
import cn.iocoder.yudao.module.happycore.dal.mysql.oxdirectory.OxDirectoryMapper;
import cn.iocoder.yudao.module.happycore.dal.mysql.oxproject.OxProjectMapper;
import cn.iocoder.yudao.module.happycore.service.ProjectFileService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;


import cn.iocoder.yudao.framework.common.pojo.PageResult;



import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.happycore.constants.ErrorCodeConstants.OX_DIRECTORY_NOT_EXISTS;


/**
 * 目录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OxDirectoryServiceImpl implements OxDirectoryService {

    @Resource
    private OxDirectoryMapper oxDirectoryMapper;
    @Resource
    private OxProjectMapper oxProjectMapper;
    @Resource
    private ProjectFileService projectFileService;

    @Override
    public Long createOxDirectory(OxDirectorySaveReqVO createReqVO) {
        // 插入
        OxDirectoryDO oxDirectory = BeanUtils.toBean(createReqVO, OxDirectoryDO.class);
        oxDirectoryMapper.insert(oxDirectory);

        // 返回
        return oxDirectory.getId();
    }

    @Override
    public void updateOxDirectory(OxDirectorySaveReqVO updateReqVO) {
        // 校验存在
        validateOxDirectoryExists(updateReqVO.getId());
        // 更新
        OxDirectoryDO updateObj = BeanUtils.toBean(updateReqVO, OxDirectoryDO.class);
        oxDirectoryMapper.updateById(updateObj);
    }

    @Override
    @Transactional
    public void deleteOxDirectory(Long id) {
        // 校验存在
        validateOxDirectoryExists(id);
        OxDirectoryDO oxDirectoryDO = oxDirectoryMapper.selectById(id);
        OxProjectDO oxProjectDO = oxProjectMapper.selectById(oxDirectoryDO.getProjectId());
        List<OxDirectoryDO> oxDirectoryDOS = oxDirectoryMapper.selectList(Wrappers.<OxDirectoryDO>lambdaQuery().eq(OxDirectoryDO::getDirectoryName, oxDirectoryDO.getDirectoryName()).eq(OxDirectoryDO::getProjectId, oxDirectoryDO.getProjectId()));
        for (OxDirectoryDO directoryDO : oxDirectoryDOS) {
            oxDirectoryMapper.deleteById(directoryDO.getId());
        }
        projectFileService.deleteProject(oxProjectDO.getProjectPath()+ DirectoryType.getPath(DirectoryType.test)+"/"+oxDirectoryDO.getDirectoryName());
        projectFileService.deleteProject(oxProjectDO.getProjectPath()+ DirectoryType.getPath(DirectoryType.train)+"/"+oxDirectoryDO.getDirectoryName());
        // 删除project中分类
        boolean remove = oxProjectDO.getCategoryList().remove(oxDirectoryDO.getDirectoryName());
        oxProjectMapper.updateById(oxProjectDO);

    }

    @Override
        public void deleteOxDirectoryListByIds(List<Long> ids) {
        // 删除
        oxDirectoryMapper.deleteByIds(ids);
        }


    private void validateOxDirectoryExists(Long id) {
        if (oxDirectoryMapper.selectById(id) == null) {
            throw exception(OX_DIRECTORY_NOT_EXISTS);
        }
    }

    @Override
    public OxDirectoryDO getOxDirectory(Long id) {
        return oxDirectoryMapper.selectById(id);
    }

    @Override
    public PageResult<OxDirectoryDO> getOxDirectoryPage(OxDirectoryPageReqVO pageReqVO) {
        return oxDirectoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OxDirectoryRespVO> getDirectoryByProjectId(Long projectId) {
        List<OxDirectoryDO> oxDirectoryDOS = oxDirectoryMapper.selectList(Wrappers.<OxDirectoryDO>lambdaQuery().eq(OxDirectoryDO::getProjectId, projectId));
        List<OxDirectoryRespVO> oxDirectoryRespVOS = BeanUtils.toBean(oxDirectoryDOS,OxDirectoryRespVO.class);
        return oxDirectoryRespVOS;
    }

}